package com.xuecheng.basic.controller;

import com.xuecheng.basic.domain.Category;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.basic.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 课程分类前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CategoryController {

    @Autowired
    private CategoryService  categoryService;

    //查询所有分类（树形结构）
    @GetMapping("/category/tree-nodes")
    public ResponseResult<CategoryVo> findAll(){
        return categoryService.findAll();
    }

    //根据id查询分类
    @GetMapping("/category/{id}")
    public ResponseResult<CategoryVo> findById(@PathVariable("id")String id){
        //查询数据库
        Category category = categoryService.getById(id);
        //转为vo
        CategoryVo categoryVo = BeanHelper.copyProperties(category, CategoryVo.class);
        //返回
        return ResponseResult.okResult(categoryVo);
    }
}
